package com.csr.csrcheck.controller;

import com.csr.csrcheck.pojo.User;
import com.csr.csrcheck.service.IUserService;
import com.csr.csrcheck.util.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping({"users"})
public class UserController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.UserController.class);

    @Resource
    private IUserService userService;

    @GetMapping({"login"})
    public JsonResult<User> login(String user_name, String password, HttpSession session) {
        User data = this.userService.login(user_name, password);
        session.setAttribute("user_id", Integer.valueOf(data.getUser_id()));
        session.setAttribute("user_name", data.getUser_name());
        session.setAttribute("role_name", data.getRole_name());
        session.setAttribute("userSession", data);
        log.info("user_id:" + getUidFromSession(session));
        return new JsonResult(Integer.valueOf(0), "json", data);
    }

    @GetMapping({"logout"})
    public JsonResult<Void> logout(HttpSession session) {
        session.removeAttribute("userSession");
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"change_password"}, method = {RequestMethod.POST})
    public JsonResult<Void> changePassword(String oldPassword, String newPassword, HttpSession session) {
        Integer userid = getUidFromSession(session);
        log.info("changePassword-------------------->userid:" + userid);
        this.userService.UpdatePassword(userid.intValue(), oldPassword, newPassword);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"change_user"}, method = {RequestMethod.POST})
    public JsonResult<Void> chageUser(String user_name, int user_sex, int user_age, String user_address, HttpSession session) {
        int id = getUidFromSession(session).intValue();
        this.userService.UpdateUser(id, user_name, user_sex, user_age, user_address);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"change_Img"}, method = {RequestMethod.POST})
    public JsonResult<Void> chageUserImg(HttpServletRequest request, MultipartFile file, HttpSession session) {
        int id = getUidFromSession(session).intValue();
        this.userService.UpdateUser_photo(id, request, session, file);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @GetMapping({"img"})
    public JsonResult<User> findImg(HttpSession session) {
        int id = getUidFromSession(session).intValue();
        User user = this.userService.finduser(id);
        return new JsonResult(Integer.valueOf(0), "json", user);
    }

    @RequestMapping(path = {"adduser"}, method = {RequestMethod.POST})
    public JsonResult<Void> addUser(String user_name, String password, int user_sex, int user_age, String user_address, HttpSession session) {
        String name = getUsernameFromSession(session);
        Date time = new Date();
        try {
            this.userService.addUser(user_name, password, user_sex, user_age, user_address, name, time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(Integer.valueOf(0), "json");
    }
}
